package components.scanario
{
	import UIFree.ObjectEvent;
	import UIFree.command.uif_commandManager;
	import UIFree.controls.uif_gbutton;
	import UIFree.controls.uif_popupMenu;
	import UIFree.uif_Timer;
	import UIFree.uif_component;
	import UIFree.uif_ecomponent;
	
	import classes.c_events;
	import classes.c_locale;
	import classes.c_uiEvents;
	
	import components.c_component;
	import components.c_image;
	import components.scanario.scenaries.c_brushScenario;
	import components.scanario.scenaries.c_frameScenario;
	
	import flash.display.DisplayObject;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	
	public class c_assistantArea extends c_component {
		private var _assistantImage	: c_assistantImage;
		private var _scenario		: c_scenarioManager;
		private var _menu			: uif_popupMenu;
		public function c_assistantArea(a_parent: uif_component, styleClass:Object='') {
			var rect: Rectangle = a_parent.getInner();
			super(a_parent, rect.x, rect.y, rect.width, rect.height, styleClass);
		}
		
		override protected function listenEvents():void {
			super.listenEvents();
//			addEvent(c_events.CANDISPATCHEVENT, checkEventScenarioElement, workspace);
//			addEvent(uif_commandManager.CANEXECUTE, checkCommandScenarioElement, workspace);
			addEvent(c_events.SETSCENARIO, doSetScenario, workspace);
			addEvent(c_uiEvents.ASSISTENT, doAssistent, workspace)
		}
		
		private function doAssistent(e: Event): void {
			_assistantImage.slideShow();
		}
		
		override protected function createComponents(): void {
			super.createComponents();
			var inner: Rectangle = app.getInner();
			_assistantImage		 = new c_assistantImage(this, inner.x + inner.width - 200, inner.y + inner.height - 200, c_images.ASSISTANT_01, style.assistant);
			_assistantImage.setVisibility(false);
			_assistantImage.addEvent(Event.CLOSE, doCloseImage);
			
			_menu = new uif_popupMenu(cl.topLayer, {
				orientation	: uif_popupMenu.LEFTPOSITION,
				itemClass	: uif_gbutton,
				showEvent	: MouseEvent.ROLL_OVER				
			}, 100, 100, 'popupmenu');
			
			_menu.activeControl = _assistantImage;
			_menu.listMenu 		= listMenu();
			_menu.addEvent(Event.SELECT, doMenuSelect);
//			_assistantImage.text = 'Привет всем!';
		}
		
		private function doMenuSelect(e: ObjectEvent): void {
			new (e.object.scenario)(workspace);
		}
		
		protected function listMenu(): Array {
			return [
				{
					caption	: 'Как вставить фотографию в рамку',
					scenario: c_frameScenario  
				},{
					caption	: 'Как перевоплотиться',
					scenario: null   
				},{
					caption	: 'Как рисовать кистью',
					scenario: c_brushScenario   
				}
			]
		}
		
		private function removeScenario(removeDesc: String=''): void {
			if (_scenario) {
				_scenario.removeEventListener(Event.CHANGE, doChangeCurElement);
				_scenario.removeEventListener(Event.COMPLETE, doComplete);
				_scenario.dispose();
				_scenario = null;
				_assistantImage.text = removeDesc;	
				uif_Timer.one(5000, function(e: Event): void {
					_assistantImage.text = '';
				});
			}
		}
		
		private function beginScenario(a_scenario: c_scenarioManager): void {
			if (_scenario) removeScenario();
			if (a_scenario != _scenario) {
				_scenario = a_scenario;
				if (_scenario) {
					_scenario.addEventListener(Event.CHANGE, doChangeCurElement);
					_scenario.addEventListener(Event.COMPLETE, doComplete);
					_scenario.curIndex 	= 0;
				}
			}
		}
		
		private function doComplete(e: Event): void {
			removeScenario(_scenario?_scenario.endDesc:'');
		}
		
		private function doChangeCurElement(e: Event): void {
			setCurDescription();
		}
		
		private function setCurDescription(): void {
			if (_scenario && _scenario.curElement) 
				_assistantImage.text = _scenario.curElement.description;
		}
		
		private function doSetScenario(e: ObjectEvent): void {
			beginScenario(e.object as c_scenarioManager);
		}
		
/*		
		private function actionNoScripted(): void {
			_assistantImage.text = c_locale.ACTIONNOSCRIPTED;
			_assistantImage.blink(1);
			uif_Timer.one(5000, function(): void {
				setCurDescription();
			});
		}
		
		private function checkCommandScenarioElement(e: ObjectEvent): void {
			if (_scenario) {
				checkElemScenary(e.object);
			}			
		}
		
		private function checkElemScenary(obj: Object): void {
			if (!_scenario.curElement.isMyObject(obj)) {
				obj.result = false;
				actionNoScripted();
			} else _scenario.nextElement();
		}
		
		
		private function checkUIEvent(e: ObjectEvent): void {
			var obj: Object = {
				type	: e.object.type,
				result	: false
			}
			workspace.dispatchEvent(new ObjectEvent(c_events.CHECKUIEVENT, obj));
			if (obj.result) {
				checkElemScenary(e.object);
			}
		}
		
		private function checkEventScenarioElement(e: ObjectEvent): void {
			if (_scenario && !c_events[e.object.type]) 
				checkUIEvent(e);
		}
*/		
		
		private function doCloseImage(e: Event): void {
			removeScenario();
		}
		
		override public function dispose():void {
			_menu.dispose();
			removeScenario();
			super.dispose();
		}
	}
}